Component({
  data: {
    showDateSidebar: false,
    selectedDate: '',
    currentDate: new Date().toISOString().split('T')[0],
    showYearPicker: false,
    showMonthPicker: false,
    showDayPicker: false,
    selectedYear: new Date().getFullYear(),
    selectedMonth: new Date().getMonth() + 1,
    selectedDay: new Date().getDate(),
    years: Array.from({length: new Date().getFullYear() - 1934}, (_,i) => 1935 + i), // 生成1935-当前年份数组
    months: [],
    days: [],
    activeSelect: null,
    showDateError: false,
    showGenderError: false,
    showEditModal: false,
    currentAvatar: '/images/touxiang.jpeg',
    selectedAvatar: null,
    avatarOptions: [
      '/images/person1.png',
      '/images/person2.png',
      '/images/touxiang.jpeg' // 包含默认头像路径
    ]
  },

  methods:{
  // 显示侧边栏
  showDateSidebar() {
    this.setData({ showDateSidebar: true });
  },
  // 隐藏侧边栏
  hideDateSidebar() {
    this.setData({ showDateSidebar: false });
  },

  // 显示年份选择
  showYearPicker() {
    this.setData({ 
      showYearPicker: true,
      showMonthPicker: false,
      showDayPicker: false
    });
  },

  // 显示月份选择
  showMonthPicker() {
    if (!this.data.selectedYear) return;
    this.setData({
      months: Array.from({length: 12}, (_,i) => i + 1),
      showYearPicker: false,
      showMonthPicker: true,
      showDayPicker: false
    });
  },

  // 显示日期选择
  showDayPicker() {
    if (!this.data.selectedMonth || !this.data.selectedYear) return;
    const daysInMonth = new Date(this.data.selectedYear, this.data.selectedMonth, 0).getDate();
    this.setData({
      days: Array.from({length: daysInMonth}, (_,i) => i + 1),
      showYearPicker: false,
      showMonthPicker: false,
      showDayPicker: true,
      selectedDay: this.data.selectedDay > daysInMonth ? 1 : this.data.selectedDay // 确保选中日期不超过当月天数
    });
  },

  // 选择年份
  selectYear(e) {
    const year = e.currentTarget.dataset.value;
    this.setData({
      selectedYear: year,
      showYearPicker: false
    });
    this.showMonthPicker();
  },

  // 选择月份
  selectMonth(e) {
    const month = e.currentTarget.dataset.value;
    this.setData({
      selectedMonth: month,
      showMonthPicker: false
    });
    this.showDayPicker();
  },

  // 选择日期
  selectDay(e) {
    const day = e.currentTarget.dataset.value;
    this.setData({
      selectedDay: day,
      showDayPicker: false
    });
  },
  
  // 新增选择方法
  selectGender(e) {
    this.setData({
      activeSelect: e.currentTarget.dataset.value
    });
  },
  
  // 保存方法
  saveData() {
    let hasError = false;
    
    if (!this.data.selectedDate) {
      this.setData({ showDateError: true });
      hasError = true;
    } else {
      this.setData({ showDateError: false });
    }

    if (!this.data.activeSelect) {
      this.setData({ showGenderError: true });
      hasError = true;
    } else {
      this.setData({ showGenderError: false });
    }

    if (hasError) return;

    // 执行保存逻辑...
    wx.showToast({ title: '保存成功' });
  },
  
  // 新增弹窗控制方法
  toggleEditModal() {
    this.setData({ showEditModal: !this.data.showEditModal });
  },
  selectAvatar(e) {
    const src = e.currentTarget.dataset.src;
    const index = this.data.avatarOptions.findIndex(item => item === src);
    if (src !== this.data.currentAvatar) {
      this.setData({
        currentAvatar: src,
        currentAvatarIndex: index,
        showEditModal: false
      });
    }
  },

  // 修改弹窗切换方法
  toggleEditModal() {
    this.setData({ 
      showEditModal: !this.data.showEditModal,
      selectedAvatar: this.data.currentAvatar // 初始化选中状态
    });
  },
  // 修改确认按钮点击方法
confirmDate() {
  if (this.data.selectedYear && this.data.selectedMonth && this.data.selectedDay) {
    const formattedDate = `${this.data.selectedYear}年${this.data.selectedMonth}月${this.data.selectedDay}日`;
    this.setData({
      selectedDate: formattedDate,
      showDateSidebar: false
    });
  }
},

// 修改取消按钮点击方法
cancelDate() {
  this.setData({ showDateSidebar: false });
}
  }

})